//绑定搜索
$(function(){

	/*-----处理下拉菜单------*/
	
	var _status = true;

	//处理下拉框
	$(".search-cont .select").click(function () {
		var _menu = $(this).attr("data-menu");
		if (typeof (_menu) == "undefined") return false;
		var _obj = $(".search-options." + _menu);
		if (_obj.length <= 0) return false;
		if (_obj.attr("data-status") == 1) return false; //菜单已展开
		_Options(_obj, $(this));
	})
	
	
	//处理选项菜单
	function _Options(o, t) {
		if (!o) return false; //对象不存在
		$(".search-options").attr("data-status",0).hide();//隐藏所有已展开的菜单
		o.show();
		o.attr("data-status", 1);
		_status = false;
		
		o.bind("mouseenter", function () {
			_status = false;
		})

		o.bind("mouseleave", function () {
			_status = true;
		})
		
		t.bind("mouseleave", function () {
			_status = true;
		})
		
		
		$("#main").bind("click", function () {
			_OptionsClose(t,o)//关闭菜单
		});

		//绑定二级菜单
		o.find("li").on("click", function () {
			if($(this).hasClass("on")) return false;
			$(this).addClass("on").siblings().removeClass("on");
			_status = true;
			_OptionsClose(t,o)//关闭菜单
			_OptionsClick(t, $(this));
			$(this).unbind("click");
		})

	}


	//关闭菜单
	function _OptionsClose(t, o) {
		if (!_status) return false;
		o.attr("data-status", 0);
		o.hide();
		//解除绑定事件
		t.unbind("mouseleave");
		o.unbind();
		$("#main").unbind();
	}

	//处理菜单点击
	function _OptionsClick(t, v) {
		if (!v || !t) return false;
		t.attr("data-id", v.attr("data-id")).html(v.html() + "<div class=\"arrow\"></div>");

		
		var datatype = $(t).attr("data-type");
		if(datatype=='1'){ //动态加载城市
			t.parents(".search-form").find(".area").val(v.attr("data-id"));
			t.parents(".search-form").find(".areapinyin").val(v.attr("data-id"));
			t.parents(".search-form").find(".city").val('');
			
			if(t.parents(".search-form").length>0){
				SoAjaxCity(v.attr("data-id"),t.parents(".search-form"));
			}else{
				SoAjaxCity(v.attr("data-id"),t.parents(".sellform"));
			}
			
		}else if(datatype=='2'){
			t.parents(".search-form").find(".city").val(v.attr("data-id"));
			t.parents(".search-form").find(".citypinyin").val(v.attr("data-id"));
		}else if(datatype=='3'){
			t.parents(".search-form").find(".cl").val(v.attr("data-id"));
		}
		
		
		
	}
	
	/*-----处理下拉菜单------*/
	
	
	var SecondaryTime = null;
	//二级菜单
	$(".Secondary-menu dl").on("mouseenter",function(){
		Secondary(true,$(this));
	})
	
	
	$(".category").on("mouseleave",function(){
		Secondary(false,$(this));

	});
	
	var Secondary = function(s,o){
		
		var i = o.index();
		
		if(s){
			$(".Secondary-menu dl").removeClass("hove");
			o.addClass("hove");
			$(".nav-shadow").css("top",o.position().top+"px").show().find(".left-box").hide().eq(i).show();
		}else{
			$(".Secondary-menu dl").removeClass("hove");
			$(".nav-shadow").hide().find(".left-box").eq(i).hide();
		}
		
	}
	
	
	
})